<?php
namespace app\member\controller;

class Money extends Member
{

    /**
     * 余额设置 2017-10-15
     */
    public function config()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $data['uid'] = UID;

            $res = dataUpdate(\tname::money_config, $data);
            if (!$res) {
                return ajaxFalse();
            }
            return ajaxSuccess();
        } else {
            $data = db(\tname::money_config)->where('uid', UID)->find();

            $this->assign('data', $data);
            return $this->fetch();
        }
    }

    /**
     * 充值卡列表 2017-10-15
     */
    public function card()
    {
        if (request()->isPost()) {
            $where = array(
                'uid' => UID,
                'ishidden' => 0
            );
            $dataList = db(\tname::money_card)->where($where)->order('sort')->paginate(500);

            $this->assign('dataList', $dataList);
            $html = $this->fetch('money/form');

            return ajaxSuccess($html);
        }
        return $this->fetch();
    }

    /**
     * 添加充值卡 2017-10-15
     */
    public function cardadd()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $data['uid'] = UID;

            $res = dataUpdate(\tname::money_card, $data);
            if (!$res) {
                return ajaxFalse();
            }
            return ajaxSuccess();
        } else {
            $id = input('param.id', 0);
            $data = db(\tname::money_card)->find($id);

            $this->assign('data', $data);
            return $this->fetch();
        }
    }

    /**
     * 订单列表 2017-10-15
     */
    public function order()
    {
        if (request()->isPost()) {
            $search = input('post.');
            $where = array(
                'uid' => UID,
                'ishidden' => 0
            );
            if ($search['stime'] && !$search['etime']) {
                $where['create_time'] = array('>= time', $search['stime']);
            }
            if (!$search['etime'] && $search['etime']) {
                $where['create_time'] = array('<= time', $search['etime']);
            }
            if ($search['etime'] && $search['etime']) {
                $where['create_time'] = array('between time', [$search['stime'], $search['etime']]);
            }
            if ($search['keyword'] !== null && $search['keyword'] !== '') {
                $where['nickname|ordernumber'] = array('like', '%' . $search['keyword'] . '%');
            }

            $dataList = db(\tname::money_order)->where($where)->order('id desc')->paginate(50, false, array('page' => $search['page']));

            $this->assign('dataList', $dataList);
            $html = $this->fetch('money/form');
            $attach = array(
                'page' => $dataList->render()
            );
            return ajaxSuccess($html, '', '', $attach);
        }

        return $this->fetch();
    }

    /**
     * 会员余额日志  2017-10-15
     */
    public function log()
    {
        if (request()->isPost()) {
            $search = input('post.');
            $where = array(
                'uid' => UID
            );
            if ($search['stime'] && !$search['etime']) {
                $where['create_time'] = array('>= time', $search['stime']);
            }
            if (!$search['stime'] && $search['etime']) {
                $where['create_time'] = array('<= time', $search['etime']);
            }
            if ($search['stime'] && $search['etime']) {
                $where['create_time'] = array('between time', [$search['stime'], $search['etime']]);
            }
            if ($search['keyword'] !== null && $search['keyword'] !== '') {
                $where['nickname|remark'] = array('like', '%' . $search['keyword'] . '%');
            }

            $dataList = db(\tname::money_log)->where($where)->order('id desc')->paginate(50, false, array('page' => $search['page']));

            $this->assign('dataList', $dataList);
            $html = $this->fetch('money/form');
            $attach = array(
                'page' => $dataList->render()
            );
            return ajaxSuccess($html, '', '', $attach);
        }

        return $this->fetch();
    }

    /**
     * 余额变动  2017-10-15
     */
    public function moneyadd()
    {
        if (request()->isPost()) {
            $data = input('post.');

            $result = moneylog(UID, 'system', $data['openid'], $data['money'], 0, $data['change'], '系统更改');
            if (!$result[0]) {
                return ajaxFalse($result[1]);
            }
            return ajaxSuccess();
        } else {
            $where_vip = array(
                'uid' => UID,
                'subscribe' => 1
            );
            $vipList = db(\tname::vip)->where($where_vip)->select();

            $this->assign('vipList', $vipList);
            return $this->fetch();
        }

    }

    /**
     * 提现列表    2017-10-15
     */
    public function withdraw()
    {
        if (request()->isPost()) {
            $search = input('post.');
            $where = array(
                'uid' => UID,
                'ishidden' => 0
            );
            if ($search['stime'] && !$search['etime']) {
                $where['create_time'] = array('>= time', $search['stime']);
            }
            if (!$search['stime'] && $search['etime']) {
                $where['create_time'] = array('<= time', $search['etime']);
            }
            if ($search['stime'] && $search['etime']) {
                $where['create_time'] = array('between time', [$search['stime'], $search['etime']]);
            }
            if ($search['keyword'] !== null && $search['keyword'] !== '') {
                $where['nickname|ordernumber'] = array('like', '%' . $search['keyword'] . '%');
            }
            if ($search['status'] !== null && $search['status'] !== '') {
                $where['status'] = $search['status'];
            }

            $dataList = db(\tname::money_withdraw)->where($where)->order('id desc')->paginate(50, false, array('page' => $search['page']));

            $this->assign('dataList', $dataList);
            $html = $this->fetch('money/form');
            $attach = array(
                'page' => $dataList->render()
            );
            return ajaxSuccess($html, '', '', $attach);
        }

        return $this->fetch();
    }

    /**
     * 提现申请到账  2017-10-15
     */
    public function withdrawcheck()
    {
        $data = input('post.');
        $withdraw = db(\tname::money_withdraw)->find($data['id']);
        if ($withdraw['status'] != 0) {
            return ajaxFalse('您已经处理过该申请');
        }
        if ($data['afterchange'] == 1) {
            db()->startTrans();
            $updatedata = array(
                'id' => $data['id'],
                'status' => 1
            );
            $res1 = dataUpdate(\tname::money_withdraw, $updatedata);
            if (!$res1) {
                db()->rollback();
                return ajaxFalse();
            }

            $config = db(\tname::weixin_config)->where(array('uid' => UID))->find();
            $pay = db(\tname::weixin_pay)->where(array('uid' => UID))->find();
            $wxpay = serializeMysql($pay['wxpay'], 1);
            $ordernumber = createOrdernumber(\tname::money_withdraw);

            $parameter = array(
                'mch_appid' => $config['appid'],    //公众平台APPID
                'mchid' => $wxpay['mchid'],        //商户号
                'partnerkey' => $wxpay['partnerkey'],    //密钥
                'money' => $withdraw['money'],    //商户平台密钥
                'openid' => $withdraw['openid'],    //用户OPENID
                'ordernumber' => $ordernumber,    //付款订单号，不能重复
                'desc' => '提现金额到账',
                'path1' => '/static/common/apiclient/' . UID . '/apiclient_cert.pem',    //证书1路径
                'path2' => '/static/common/apiclient/' . UID . '/apiclient_key.pem'    //证书2路径
            );
            $Pay = new \wechat\Pay();
            $result = $Pay->pay($parameter);

            if ($result['result_code'] === 'SUCCESS') {
                $updatedata = array(
                    'id' => $data['id'],
                    'isarrival' => 1,
                    'arrivaltime' => time(),
                    'ordernumber' => $ordernumber
                );
                $res2 = dataUpdate(\tname::money_withdraw, $updatedata);
                if (!$res2) {
                    db()->rollback();
                    return ajaxFalse();
                }
            } else {
                db()->rollback();
                return ajaxFalse($result['err_code_des']);
            }

            db()->commit();
            return ajaxSuccess();
        } elseif ($data['afterchange'] == -1) {
            $result = moneylog(UID, 'withdraw_fail', $withdraw['openid'], $withdraw['money'], $data['id'], 1, '提现失败退还');

            $updatedata = array(
                'id' => $data['id'],
                'status' => -1,
                'check_time' => time()
            );
            $res = dataUpdate($data['tablename'], $updatedata);
            if (!$res) {
                return ajaxFalse();
            }
            return ajaxSuccess();
        }

    }
}